package org.maxgroundstation.groundstation.comm.service.geometry;

import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.swing.SwingUtilities;

import org.maxgroundstation.groundstation.comm.service.Service;
import org.maxgroundstation.groundstation.comm.service.ServiceCommunication;
import org.maxgroundstation.groundstation.comm.service.ServiceDataFrame;
import org.maxgroundstation.groundstation.comm.service.ServiceUtil;

/**
 * The rover geometry service.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class GeometryService extends Service {

    // Static members
    public static final String SERVICE_ID = ServiceUtil.getServiceID("geometry");
    public static final String TYPE = "geometry";
    
    // Service event type.
    public static final String GEOMETRY_UPDATE = "geometry update";
    
    // Data members
    private Map<String, Float> geometryMap;
    
    /**
     * Constructor
     * @param comm the service communication.
     */
    public GeometryService(ServiceCommunication comm) {
        // Use Service constructor
        super(comm);
        
        // Initialize data members.
        geometryMap = new HashMap<String, Float>();
    }
    
    /**
     * Gets the geometry map.
     * @return geometry map.
     */
    public Map<String, Float> getGeometryMap() {
        return new HashMap<String, Float>(geometryMap);
    }
    
    @Override
    public String getID() {
        return SERVICE_ID;
    }

    @Override
    public void handleResponse(ServiceDataFrame dataFrame) {
        // Parse data string.
        String dataString = dataFrame.getDataString().trim();
        //System.out.println("Geometry update: " + dataString);
        StringTokenizer st1 = new StringTokenizer(dataString, ",");
        while(st1.hasMoreTokens()) {
            String keyValuePair = st1.nextToken();
            StringTokenizer st2 = new StringTokenizer(keyValuePair, "=");
            String key = st2.nextToken();
            String valueString = st2.nextToken();
            float value = Float.parseFloat(valueString);
            geometryMap.put(key, value);
        }
        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                fireServiceEvent(GEOMETRY_UPDATE, new HashMap<String, Float>(geometryMap));
            }
        });
    }

    @Override
    public void initialize() {
        // Do nothing
    }

    @Override
    public boolean isDefaultService() {
        return false;
    }

    @Override
    public boolean isSubscribable() {
        return true;
    }

    @Override
    public String getType() {
        return TYPE;
    }
}